package com.evil.account.enums.log;

import com.evil.account.pojo.entity.*;
import com.evil.common.log.enums.base.LogType;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Getter
@AllArgsConstructor
public enum AccountLogTypeEnum implements LogType {

    USER(2000, "用户", User.class),

    DEPARTMENT(2100, "部门", Department.class),
    USER_DEPARTMENT(2101, "用户部门", UserDepartment.class),

    ROLE(2200, "角色", Role.class),
    USER_ROLE(2201, "用户角色", UserRole.class),

    ENTERPRISE(3000, "企业", Enterprise.class),
    USER_ENTERPRISE(3002, "用户企业", UserEnterprise.class),
    ;

    private final int type;
    private final String name;
    private final Class<?> entityClass;

    public static Optional<AccountLogTypeEnum> isExistByType(Integer type) {
        if (null == type) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.type == type).findFirst();
    }

    public static Optional<AccountLogTypeEnum> isExistByClass(Class<?> entityClass) {
        if (null == entityClass) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> null != e.entityClass && e.entityClass.equals(entityClass)).findFirst();
    }

    public static AccountLogTypeEnum findByClass(Class<?> entityClass) {
        return AccountLogTypeEnum.isExistByClass(entityClass).orElseThrow(() -> LogType.error(entityClass));
    }
}
